import {Result} from '@/base/Result';
import {request} from '@/base/Request';
import {Page} from '@/base/Page';
import {Info} from '@/base/model/Info';
import {TableSort} from '@/base/TableSort';
import qs from 'qs';

export function getInfoList(params: {
  pageIndex: number,
  pageSize: number,
  sortId?: number,
  title?: string,
  companyId?: number[]
}): Promise<Result<Page<Info>>> {
  Object.assign(params, TableSort());
  let companyIdString = '';
  if (params.companyId && params.companyId.length > 0) {
    companyIdString = params.companyId.map(x => 'companyId=' + x).join('&');
    delete params.companyId;
  }
  return request.getPage(Info, '/api/info?' + companyIdString, {params});
}

export function getInfo(id: number): Promise<Result<Info>> {
  return request.get(Info, `/api/info/${id}`);
}

export function putInfo(id: number, params: {
  body: string,
  sortId: number,
  images: string[],
  title: string,
  enable?: boolean
}): Promise<Result<Info>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(Info, `/api/info/${id}`, formData);
}

export function putInfoEnable(params: {
  id: number,
  enable: boolean
}): Promise<Result<Info>> {
  return request.put(Info, '/api/info/enable', qs.stringify(params));
}

export function postInfo(params: {
  body: string,
  sortId: number,
  images: string[],
  title: string
}): Promise<Result<Info>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Info, '/api/info', formData);
}

export function deleteInfo(id: number): Promise<Result<void>> {
  return request.delete(`/api/info/${id}`);
}
